package org.jgs1904.test;

import java.io.IOException;
import java.sql.SQLOutput;

/**
 * @author: 默苍璃
 * @date: 2022-12-23 15:36
 * <p>
 * 在代码中获取原始异常可以使用Throwable.getCause()方法。如果返回null，说明已经是“根异常”了。
 */
public class GetCause {

    public static void main(String[] args) {
        try {
            process1();
        } catch (Exception e) {
            e.printStackTrace();

            Throwable cause = e.getCause();
            System.out.println("main");
            System.out.println(cause);
        }
    }


    static void process1() {
        try {
            process2();
        } catch (NullPointerException e) {
            System.out.println(111);
            Throwable cause = e.getCause();
            System.out.println(cause);
            throw new IllegalArgumentException(e);
        }

    }

    static void process2() {
        try {
            process3();
        } catch (ArrayIndexOutOfBoundsException e) {
            Throwable cause = e.getCause();
            System.out.println(222);
            System.out.println(cause);
            NullPointerException nullPointerException = new NullPointerException();
            nullPointerException.initCause(e);
            throw nullPointerException;
        }

    }

    static void process3() {
        throw new ArrayIndexOutOfBoundsException();
    }


    static void process4() {
        try {
            process5();
        } catch (IllegalArgumentException e) {
            NullPointerException nullPointerException = new NullPointerException();
            nullPointerException.initCause(e);
            throw nullPointerException;
        }
    }

    static void process5() {
        throw new IllegalArgumentException();
    }


}


